﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;

namespace Apewer.Network
{

    /// <summary>扩展方法。</summary>
    public static class Extension
    {

        /// <summary>添加邮件账户。</summary>
        public static void Add(this List<MailAddress> value, string address, string name)
        {
            if (value == null) return;
            value.Add(new MailAddress(address, name));
        }

        /// <summary>添加邮件账户。</summary>
        public static void Add(this List<MailAddress> value, string address)
        {
            if (value == null) return;
            value.Add(new MailAddress(address));
        }

        /// <summary>发送邮件。</summary>
        public static MailRecord Send(this MailClient value, MailMessage message)
        {
            return MailMethods.Send(value, message);
        }

        /// <summary>发送邮件。</summary>
        public static MailRecord Send(this MailClient value, string sender, string receiver, string content = null, string title = null)
        {
            return MailMethods.Send(value, sender, receiver, content, title);
        }

        /// <summary>生成 Json 数组。</summary>
        static Json ToJsonArray<T>(this IEnumerable<T> items, Func<T, Json> serializer)
        {
            if (items == null) return null;

            var array = Apewer.Json.NewArray();
            if (serializer != null)
            {
                foreach (var item in items)
                {
                    if (item == null) continue;
                    var json = serializer.Invoke(item);
                    array.AddItem(json);
                }
            }
            return array;
        }

        /// <summary>生成 Json 对象。</summary>
        public static Json ToJson(this IPAddress address)
        {
            if (address == null) return null;
            var json = Json.NewObject();
            json.SetProperty("text", address.ToString());
            json.SetProperty("family", address.AddressFamily.ToString());
            if (address.AddressFamily == AddressFamily.InterNetworkV6)
            {
                json.SetProperty("isLinkLocal", address.IsIPv6LinkLocal);
                json.SetProperty("scopeId", address.ScopeId);
            }
            return json;
        }

        /// <summary>生成 Json 数组。</summary>
        public static Json ToJson(this IEnumerable<IPAddress> addresses) => ToJsonArray(addresses, ToJson);

        /// <summary>生成 Json 数组。</summary>
        public static Json ToJson(this NetworkInterface @interface)
        {
            if (@interface == null) return null;
            var item = @interface;

            var json = Json.NewObject();
            json.SetProperty("text", item.ToString());

            json.SetProperty("description", item.Description);
            json.SetProperty("id", item.Id);
            json.SetProperty("isReceiveOnly", item.IsReceiveOnly);
            json.SetProperty("name", item.Name);
            json.SetProperty("type", item.NetworkInterfaceType.ToString());
            json.SetProperty("operational", item.OperationalStatus.ToString());
            json.SetProperty("speed", item.Speed);
            json.SetProperty("multicast", item.SupportsMulticast);

            json.SetProperty("mac", item.GetPhysicalAddress().ToString());
            json.SetProperty("ipProperties", ToJson(item.GetIPProperties()));

            return json;
        }

        /// <summary>生成 Json 数组。</summary>
        public static Json ToJson(this IEnumerable<NetworkInterface> items) => ToJsonArray(items, ToJson);

        /// <summary>生成 Json 对象。</summary>
        public static Json ToJson(this IPInterfaceProperties properties)
        {
            if (properties == null) return null;
            var json = Json.NewObject();
            json.SetProperty("anycast", ToJson(properties.AnycastAddresses.Select(x => x.Address)));
            json.SetProperty("dhcp", ToJson(properties.DhcpServerAddresses));
            json.SetProperty("dns", ToJson(properties.DnsAddresses));
            json.SetProperty("suffix", properties.DnsSuffix);
            json.SetProperty("gateway", ToJson(properties.GatewayAddresses.Select(x => x.Address)));
            json.SetProperty("multicast", ToJson(properties.MulticastAddresses.Select(x => x.Address)));
            json.SetProperty("unicast", ToJson(properties.UnicastAddresses.Select(x => x.Address)));
            json.SetProperty("wins", ToJson(properties.WinsServersAddresses));
            return json;
        }

        /// <summary>生成 Json 对象。</summary>
        public static Json ToJson(this IPAddressInformation information)
        {
            if (information == null) return null;
            var json = Json.NewObject();
            json.SetProperty("address", ToJson(information.Address));
            json.SetProperty("isDnsEligible", information.IsDnsEligible);
            json.SetProperty("isTransient", information.IsTransient);
            return json;
        }

        /// <summary>生成 Json 对象。</summary>
        public static Json ToJson(this PingReply reply)
        {
            if (reply == null) return null;

            var buffer = BytesUtility.ToX2(reply.Buffer);
            if (buffer.Replace("0", "").IsEmpty()) buffer = null;

            var json = Json.NewObject();
            json.SetProperty("address", ToJson(reply.Address));
            if (buffer != null) json.SetProperty("buffer", buffer);
            json.SetProperty("options", ToJson(reply.Options));
            json.SetProperty("roundtripTime", reply.RoundtripTime);
            json.SetProperty("status", reply.Status.ToString());
            return json;
        }

        /// <summary>生成 Json 对象。</summary>
        public static Json ToJson(this PingOptions options)
        {
            if (options == null) return null;
            var json = Json.NewObject();
            json.SetProperty("dontFragment", options.DontFragment);
            json.SetProperty("ttl", options.Ttl);
            return json;
        }

    }

}
